using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameStateManagement;
using Microsoft.Xna.Framework.Input.Touch;


namespace UpChickenFar
{
    
    public class Rotor : DrawableGameComponent
    {
        Texture2D _rotor_texture;
        float _start_rotate = 0.0f;
        float _speed_rotate = 0.1f;
        Vector2 _positon;
        SpriteBatch _spriteBatch;
        public event EventHandler<EventArgs> Selected;
        private Rectangle _bound_rect;
        private float _rotor_scale = 1;

        public Rotor(Game game, Vector2 position, Texture2D rotor, float scale, float speed)
            : base(game)
        {
            _spriteBatch = UpChickenFar.spriteBatch;
            _positon = position;
            _rotor_texture = rotor;
            _rotor_scale = scale;
            _speed_rotate = speed;
            _bound_rect = new Rectangle();
            _bound_rect.X = (int)(_positon.X - rotor.Width / 2);
            _bound_rect.Y = (int)(_positon.Y - rotor.Height / 2);
            _bound_rect.Width = rotor.Width;
            _bound_rect.Height = rotor.Height;
        }

        public void HandleInput(GameTime gameTime, InputState input)
        {
            foreach (GestureSample gesture in input.Gestures)
            {
                if (gesture.GestureType == GestureType.Tap)
                {
                    if (IsPointInRectangle(gesture.Position, _bound_rect))
                    {
                        if (Selected != null)
                        {
                            //_speed_rotate += 0.1f;
                        }
                    }
                }
            }
        }
        private bool IsPointInRectangle(Vector2 point, Rectangle rect)
        {
            if (point.X >= rect.Left && point.X <= rect.Right && point.Y >= rect.Top && point.Y <= rect.Bottom)
            {
                return true;
            }
            return false;
        }

        public override void Update(GameTime gameTime)
        {
            _start_rotate += _speed_rotate ;
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin();
            _spriteBatch.Draw(_rotor_texture, _positon, null, Color.White, _start_rotate, new Vector2(_rotor_texture.Width / 2, _rotor_texture.Height / 2), _rotor_scale, SpriteEffects.None, 0);
            _spriteBatch.End();
            base.Draw(gameTime);
        }
        
    }
}
